function sendComment (id)
{
	var nom = document.getElementById("commentName").value;
	var email = document.getElementById("commentEmail").value;
	var web = document.getElementById("commentWeb").value;
	var spam = document.getElementById("commentSpam").value;
	var texte = document.getElementById("commentText").value;
	if(nom=="") alert("Il faut un nom !");
	else if(spam!="plop") alert("Il faut mettre plop dans la case antispam !");
	else if(texte=="") alert("Il faut un message !");
	else
	{
		document.getElementById("sendCommentButton").value = "Loading...";
		document.getElementById("sendCommentButton").disabled = true;
		if(document.getElementById("save").checked)
		{
			ecrire_cookie("nom",nom);
			ecrire_cookie("email",email);
			ecrire_cookie("web",web);
		}
		nom = textEncode(nom);
		email = textEncode(email);
		texte = textEncode(texte);
		c_request = createAjax( function(r)
		{
			if(r.length>2 && r.substr(0,2)=="OK")
				document.getElementById("myComment").innerHTML = r.substring(2);
			else
			{
				alert(r);
				document.getElementById("sendCommentButton").value = "Envoyer";
				document.getElementById("sendCommentButton").disabled = false;
			}
		},"addcomment.php");
		c_request.send("id="+id+"&nom="+nom+"&email="+email+"&web="+web+"&texte="+texte);
	}
}

function createAjaxGeneral (finishFunction)
{
	var request = false;
	// creation de la requete
	if(window.XMLHttpRequest)
	{
		request = new XMLHttpRequest();
		if(request.overrideMimeType) { request.overrideMimeType('text/xml'); }
	}
	else if(window.ActiveXObject)
	{
		try { request = new ActiveXObject("Msxml2.XMLHTTP"); }
		catch(e) { try { request = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} }
	}
	if(!request)
	{
		alert('Giving up :(\nCannot create an XMLHTTP instance');
		return false;
	}
	// creation de la fonction de fin
	var f = function()
	{
		if(request.readyState==4)
		{
			if(request.status==200)
			{
				if(request.responseText=="") alert("Pas de réponse");
				else finishFunction(request.responseText);
			}
			else alert("erreur");
		}
	}
	// parametrage de la requete
	request.onreadystatechange = f;
	return request;
}

function createAjax (finishFunction, php)
{
	request = createAjaxGeneral (finishFunction)
	request.open('POST',php,true);
	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	return request;
}

function getAjax (finishFunction, file)
{
	request = createAjaxGeneral (finishFunction)
	request.open('GET',file,true);
	request.send(null);
}

// ENCODE TEXTE
function textEncode(text)
{
	var r = encodeURI(text);
	r = r.replace(/\+/g, '%2B');
	r = r.replace(/\*/g, '%2A');
	r = r.replace(/\//g, '%2F');
	r = r.replace(/@/g, '%40');
	r = r.replace (/&/g, '%26');
	return r;
}
